﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Gollie.DataTables;

namespace Gollie.Controls.Admin
{
	public partial class PaymentToVendor : AdminRequiredBaseUserControl
	{
		PaymentsToVendorData paymentToVendorData;

		protected override void OnFirstHit(object sender, EventArgs e)
		{
			DateTime period = DateTime.Today;
			this.YearPickerControl.YearHigh = period.Year;
			this.YearPickerControl.NumberOfYears = 10;

			period = period.AddMonths(-1);
			
			this.MonthPickerControl.SelectedMonth = period.Month;
			this.YearPickerControl.SelectedYear = period.Year;

			this.Refresh();
		}

		private bool IsRefreshed()
		{
			bool isRefreshed = (this.lblPeriodMonth.Text == this.MonthPickerControl.SelectedMonthText
				&& this.lblPeriodYear.Text == this.YearPickerControl.SelectedYear.ToString());

			if (!isRefreshed)
				this.tbIsRefreshed.Value = "0";
			else
				this.tbIsRefreshed.Value = "1";

			return isRefreshed;
		}

		private void Refresh()
		{
			this.lblPeriodMonth.Text = this.MonthPickerControl.SelectedMonthText;
			this.lblPeriodYear.Text = this.YearPickerControl.SelectedYear.ToString();

			paymentToVendorData = new PaymentsToVendorData(this.MonthPickerControl.SelectedMonth, this.YearPickerControl.SelectedYear);

			this.tbIsRefreshed.Value = "1";
			this.GridView1.DataSource = paymentToVendorData;

			this.GridView1.DataBind();
		}

		protected SortDirection SortDir
		{
			get
			{
				if (ViewState["dirState"] == null)
					ViewState["dirState"] = SortDirection.Ascending;

				return (SortDirection)ViewState["dirState"];
			}
			set
			{
				ViewState["dirState"] = value;
			}
		}

		protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
		{
			SortDir = SortDir == SortDirection.Ascending ? SortDirection.Descending : SortDirection.Ascending;
			paymentToVendorData = new PaymentsToVendorData(this.MonthPickerControl.SelectedMonth, this.YearPickerControl.SelectedYear);
			this.GridView1.DataSource = paymentToVendorData.Sort(e.SortExpression, SortDir == SortDirection.Ascending);
			this.GridView1.DataBind();
		}

		protected void GridView1_DataBinding(object sender, EventArgs e)
		{
			this.GridView1.Columns[1].FooterText = ObjectFormatter.GetCurrencyString(paymentToVendorData.totalDeposits);
			this.GridView1.Columns[2].FooterText = ObjectFormatter.GetCurrencyString(paymentToVendorData.totalDebt);
			this.GridView1.Columns[3].FooterText = ObjectFormatter.GetCurrencyString(paymentToVendorData.totalPaid);
			this.GridView1.Columns[4].FooterText = ObjectFormatter.GetCurrencyString(paymentToVendorData.totalDebtLeft);
		}

		protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
		{
			if (e.Row.RowType == DataControlRowType.DataRow)
			{
				uint vendorId = Convert.ToUInt32(GridView1.DataKeys[e.Row.RowIndex].Value);

				decimal leftToPay = paymentToVendorData.GetAmountToPay(vendorId);

				if (leftToPay <= (decimal)0.01)
				{
					Control c = e.Row.FindControl("btnRegisterPayment");
					if (c != null)
						c.Visible = false;
				}
			}
		}

		protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
		{
			switch (e.CommandName)
			{
				case "RegisterPayment":
					if (this.IsRefreshed())
					{
						uint vendorId = Convert.ToUInt32(e.CommandArgument);

						paymentToVendorData = new PaymentsToVendorData(this.MonthPickerControl.SelectedMonth, this.YearPickerControl.SelectedYear);

						this.paymentToVendorData.RegisterPayment(vendorId);

						this.Refresh();
					}
					break;
			}
		}

		protected void btnPeriod_Click(object sender, EventArgs e)
		{
			this.Refresh();
		}

		protected void btnRegisterAllPayments_Click(object sender, EventArgs e)
		{
			if (this.IsRefreshed())
			{
				paymentToVendorData = new PaymentsToVendorData(this.MonthPickerControl.SelectedMonth, this.YearPickerControl.SelectedYear);
				this.paymentToVendorData.RegisterAllPayments();

				this.Refresh();
			}
		}

	}
}